/***************************************************************************
    begin                : jan 01 2013
    copyright            : (C) 2013 by Benoit Valot
    email                : benvalot@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 23 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

package val.lemonde;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class FragmentConfigurationCatSelected extends FragmentLemondeBase
		implements OnItemClickListener {

	protected ListView maListViewPerso;

	private ArrayList<Categorie> cats;

	protected Parcelable state;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.list_view, container, false);
		maListViewPerso = (ListView) view.findViewById(R.id.listviewperso);
		this.load_content();
		return (view);
	}

	public void restoreDefault() {
		// Do to restore default
	}

	public void load_content() {
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map;
		// Chargement des catégories
		this.cats = this.get_datas().get_categorie(false);
		// on les trie
		Collections.sort(cats);

		for (Categorie cat : cats) {
			map = new HashMap<String, String>();
			map.put("titre", cat.getName());
			if (cat.isVisible()) {
				map.put("color", "#aa0000");
			} else {
				map.put("color", "#47637f");
			}
			map.put("check", "checked-" + cat.isVisible());
			listItem.add(map);
		}

		// Création d'un SimpleAdapter qui se chargera de mettre les items
		// présent dans notre list (listItem) dans la vue affichageitem
		SimpleAdapter mSchedule = new SimpleAdapter(this.getActivity()
				.getBaseContext(), listItem, R.layout.categorie_listes,
				new String[] { "titre", "color", "check" }, new int[] {
						R.id.cat_title, R.id.cat_color, R.id.cat_image });
		// on sauve
		state = maListViewPerso.onSaveInstanceState();

		mSchedule.setViewBinder(new BindColorAndImage());

		maListViewPerso.setAdapter(mSchedule);

		maListViewPerso.setOnItemClickListener(this);
		
		maListViewPerso.onRestoreInstanceState(state);
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		Log.d("lemonde", "ChangeCatAt=" + position);
		this.cats.get(position)
				.setVisible(!this.cats.get(position).isVisible());
		this.get_datas().save_categorie_order(cats);
		this.load_content();
	}

}
